<?php

namespace front\modules\app\controllers;

use common\WebController;
use Yii;
use front\models\CategoryModel;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use goods\services\CategoryService;
use goods\services\CategoryPropertyService;
use yii\helpers\ArrayHelper;
use front\controllers\WebBaseController;
/**
 * CategoryController implements the CRUD actions for CategoryModel model.
 */
class CategoryController extends WebBaseController
{
    private $cid;
    private $pid;
    function init(){
        parent::init();
        $this->cid = intval(Yii::$app->request->get('cid'));
        if(!$this->cid){
            WebBaseController::getAjaxData(2,'类目不存在');
        }
        $this->pid = Yii::$app->request->get('cid') ? intval(Yii::$app->request->get('cid')):'';
    }
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 查出根类目
     *
     */
    public function actionIndex()
    {
        //获取根类目
       $getRootType = CategoryService::getRoot();
       if(!$getRootType['code']==1){
           WebBaseController::getAjaxData(3,$getRootType['message']);
       }
        WebBaseController::getAjaxData(1,'获取成功',$getRootType);
    }
    /*
     *查询下级类目
     */
    function actionSubcategory(){
        $getSubCategory = CategoryService::getSubcategory($this->cid);
        if($getSubCategory['code']==1){
            WebBaseController::getAjaxData(3,$getSubCategory['message']);
        }
        if($getSubCategory['result']==2){
            WebBaseController::getAjaxData(4,$getSubCategory['message']);
        }
        WebBaseController::getAjaxData(5,'获取成功',$getSubCategory);
    }

    /**
     * 获取根目录属性值
     */
    function actionRootbrand(){
        $getRootBrand = CategoryService::getBrand($this->cid);
        if($getRootBrand['code']==0){
            if(!is_array($getRootBrand['result'])){
                WebBaseController::getAjaxData(3,$getRootBrand['message'],$getRootBrand['result']);
            }
            if(is_array($getRootBrand['result'])){
                WebBaseController::getAjaxData(4,$getRootBrand['message'],$getRootBrand['result']);
            }
        }else{
            WebBaseController::getAjaxData(5,$getRootBrand['message'],$getRootBrand['result']);
        }
    }
    /**
     * 获取对应的属性值
     */
    function actionGetbrandlist(){
        if(!$this->pid){
            WebBaseController::getAjaxData(3,'属性不存在');
        }
        $getBarnd = CategoryService::getModel($this->cid,$this->pid);
        if($getBarnd['code']==1){
            WebBaseController::getAjaxData(1,$getBarnd['message'],$getBarnd['result']);
        }
        if($getBarnd['code']==0){
            if(!is_array($getBarnd['result'])){
                WebBaseController::getAjaxData(4,$getBarnd['message'],$getBarnd['result']);
            }else{
                WebBaseController::getAjaxData(5,$getBarnd['message'],$getBarnd['result']);
            }

        }
    }

    /**
     * 根据cid查询类目对应的信息
     */
    function actionGetnamelist(){
        $getInfo = CategoryService::getName($this->cid);
        if($getInfo['code']==1){
            WebBaseController::getAjaxData(3,$getInfo['message'],$getInfo['result']);
        }
        if($getInfo['code']==0){
          $arr = ArrayHelper::toArray($getInfo['result']);
            WebBaseController::getAjaxData(4,$getInfo['message'],$arr);
        }

    }

    /**
     *获取指定类目对应属性
     */

    function actionGetproperty(){
        $info = CategoryPropertyService::getCategoryProperty($this->cid);
        if($info['code']==0){
            if(is_array($info['result'])){
                WebBaseController::getAjaxData(3,$info['message'],$info['result']);
            }else{
                WebBaseController::getAjaxData(4,$info['message'],$info['result']);
            }
        }
    }

    /**
     * 获取指定属性对应的子属性和对应的子属性值
     */
    function actionGetchildProperty(){
        if(!$this->pid){
            WebBaseController::getAjaxData(3,'属性不存在');
        }
        $info = CategoryPropertyService::getChildProperty($this->cid,$this->pid);
        if($info['code']==0){
            WebBaseController::getAjaxData(4,$info['message'],$info['result']);
        }elseif($info['code']==1){
            WebBaseController::getAjaxData(5,$info['message'],$info['result']);
        }
    }



    /**
     * Displays a single CategoryModel model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new CategoryModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new CategoryModel();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->cid]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing CategoryModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->cid]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing CategoryModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the CategoryModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return CategoryModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CategoryModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
